import { observable, action, asMap } from 'mobx';
import * as API from '../apis/form';
import base from "../util/base";
import { buildUrlStr } from '../util/forwardPage';
export class  wfSmartGuessStore{
  @observable smartGuessStatus = false; //智能预测功能
  @observable allMapshow = false; //完整视图
  @observable needShowTip = false;
  @observable forecastInfoMap = observable.map ? observable.map({}) : asMap({}); 
  @observable clickedMap = observable.map ? observable.map({}) : asMap({}); 
  @observable baseReqParams = {};
  
  @observable WholeMapPicSrc = '';
  @observable smartGuessPicSrc = '';
  @observable forecastMapSrc = '';

  @observable needBtnMask = false;
  @observable isReset = false;
  @observable justDoOnce = true;
  @observable ensuresubmitInfo = {};
  @observable isFromDialog = false;
  @observable showDescriptWord = false;

  allViewRecordOnce = true;
  @observable smartBtnFirstClick = false;
  @observable allViewBtnFirstClick = false;
  submitParam = {};
  recordScaleVal = 1;
  timer = null;


  @action('修改变量值')
   changeState = (obj = {}) => {
    const keys = Object.keys(obj);
    for(let key of keys) {
      this[key] = obj[key];
    }
  }
  @action("记录弹框submitParam值")
  setSubmitParam = (obj = {}) => {
    this.submitParam = obj;
  }
  @action('记录按钮被点击')
  setRecordClicked = (key) => {
    if(!this.clickedMap.has(key))
      this.clickedMap.set(key, true);
  }
  
  switchStatus = {
    'openAutoForecast': '0',
    'showAutoForecastResult': '0'
  }
  @action initParams = (params = {}) =>  {
    let logParams = {};
    const {hasFreeNode = false} = params;
    const urlParam = {
      requestid:params.requestid,
      workflowid:params.workflowid,
      isbill:params.isbill,
      formid:params.formid,
      nodeid:params.nodeid,
      isfromtab:params.isfromtab,
      f_weaver_belongto_userid:params.f_weaver_belongto_userid,
      f_weaver_belongto_usertype:params.f_weaver_belongto_usertype,
      showE9Pic: "1",
      isFromWfForm:true,
      authSignatureStr:params.authSignatureStr,
      authStr:params.authStr,
      isFree:params.isFree,
      isAllowNodeFreeFlow:params.isAllowNodeFreeFlow,
      isReadOnlyModel:'true',
      hasFreeNode:hasFreeNode ? "1" :"0",
      isMobile:'1',
    };
    const {rightMenu = {}} = base.wfOperateStore;
    const {isFree = '0',isAllowNodeFreeFlow = '0'} = rightMenu;
    urlParam['isFree'] = isFree;
    urlParam['isAllowNodeFreeFlow'] = isAllowNodeFreeFlow;
    let baseParamKey = ["requestid", "requestType", "workflowid", "nodeid","f_weaver_belongto_userid",
    "f_weaver_belongto_usertype", "authSignatureStr", "authStr", "isFree", "currentnodetype"];
    const switchParamsKey = ['openAutoForecast', 'showAutoForecastResult'];

    baseParamKey.map( v => {logParams[v] = params[v]});
    switchParamsKey.map( v => { this.switchStatus[v] = params[v]});
    
    if(this.submitParam.requestid) {
      this.baseReqParams = {...this.baseReqParams, ...logParams, ...this.submitParam};
    } else {
      this.baseReqParams = {...this.baseReqParams, ...logParams };
    }
    urlParam.requestid = this.baseReqParams.requestid;
    
    if( this.baseReqParams.requestid 
      && this.baseReqParams.requestid != -1  
      && this.baseReqParams.isFree 
      && this.baseReqParams.isFree != 1 
      && this.baseReqParams.currentnodetype 
      && this.baseReqParams.currentnodetype != 3
      && this.switchStatus.openAutoForecast 
      && this.switchStatus.openAutoForecast == '1') {
        this.openForecastFunc(true);
        if(this.switchStatus.showAutoForecastResult 
          && this.switchStatus.showAutoForecastResult.indexOf('2') > -1) {
            this.clickSmartBtn();
          }
    }
    this.WholeMapPicSrc = `/workflow/workflowDesign/m-readOnly.html?v=20200312&isFromWfForm=true&${buildUrlStr(urlParam)}`;
    this.smartGuessPicSrc = `/spa/workflow/static4form/index.html?v=20200312&_rdm=1573808462100#/main/workflow/smartGuess?${buildUrlStr(this.baseReqParams)}&isFromMobile=1`;
    this.forecastMapSrc =  `/workflow/workflowDesign/m-readOnly.html?v=20200312&isFromWfForm=true&${buildUrlStr(urlParam)}&showForecastNode=1`;
  }
  
  @action('开启智能预测功能')
  openForecastFunc = bool => {
    this.needForecastFunc = bool;
  }


  @action('设置弹框提示内容') 
  setPromptInfo = ( info = {}) => {
    this.ensureSubmitInfo =  info;
  }
  @action('获取预测者信息') 
  getForecastInfo = (val) => {
    let  requestid = val;
    let params = {requestid: val}
    API.getForecastInfo(params).then( data => {
      if(data.forecastInfo) {
        this.forecastInfoMap.set(requestid,  data.forecastInfo);
      }
      this.needShowTip = data.showTip;
    })
  }
 
  @action('模拟智能预测按钮点击的转态')
  clickSmartBtn = () => {
    this.smartGuessStatus = true;
    this.allMapshow = false;
    this.setRecordClicked('smart-btn-click');
    this.justDoOnce = false;
    this.getForecastInfo(this.baseReqParams.requestid);
    this.isFromDialog = true;
    this.needBtnMask = true;
  }
  @action('重新预测')
  getResetForecast = () => {
    this.smartGuessPicSrc = `${this.smartGuessPicSrc}&resetForecast=1`
  }
  @action('清理store')
  clearStore = () => {
    this.needForecastFunc = false;
    this.recordScaleVal = 0;
    this.smartGuessStatus = false;
    this.allMapshow = false;
    this.clickedMap = observable.map ? observable.map({}) : asMap({}); 
    this.forecastInfoMap = observable.map ? observable.map({}) : asMap({}); 
    this.smartGuessPicSrc = '';
    this.WholeMapPicSrc = '';
    this.forecastMapSrc = '';
    this.baseReqParams = {}; 
    this.justDoOnce = true;
    this.isFromDialog = false;
  }

  @observable dialog = null;
  @action('记录弹框')
  setDialogClose = ( obj ) => {
    this.dialog = obj;
  }
}